package model

import (
	"bytes"
	"encoding/gob"
	"math/rand"
	"time"
)

const ()

var ()

type Head struct {
	Sender    string
	Receiver  string
	Trans     []string
	TimeStamp int64
	Sign      []byte
	Lengh     int
}

type ResponseHandle func(data *TranInfo, sender string) error

type ResponseInfo struct {
	Receiver  string
	SignCheck bool
	ReqTime   time.Time
	Handle    ResponseHandle
}

func Encode(data interface{}) ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	enc := gob.NewEncoder(buf)
	err := enc.Encode(data)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

func Decode(data []byte, to interface{}) error {
	buf := bytes.NewBuffer(data)
	dec := gob.NewDecoder(buf)
	return dec.Decode(to)
}

//生成随机字符串
func GetRandomString(lengh int) string {
	str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	bytes := []byte(str)
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < lengh; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}
